package com.zhsq.zlc.controller.authority;

import com.zhsq.zlc.entity.IDEntity;
import com.zhsq.zlc.entity.LogEntity;
import com.zhsq.zlc.entity.ResultObject;
import com.zhsq.zlc.entity.AuthorityEntity;
import com.zhsq.zlc.service.authority.AuthorityService;
import com.zhsq.zlc.service.combobox.ComboboxService;
import com.zhsq.zlc.service.log.LogService;
import com.zhsq.zlc.util.Constant;
import com.zhsq.zlc.util.PageUtil;
import com.zhsq.zlc.util.SessionInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class AuthorityController {

	@Autowired
	private AuthorityService authorityService;

	@Autowired
	private ComboboxService comboboxService;

	@Autowired
	private LogService logService;
	/**
	 * 权限列表
	 * @param entity
	 * @param pageCurrent
	 * @param pageSize
	 * @param pageCount
	 * @param model
	 * @return
	 */
	@RequestMapping("/zhsq/authorityManage_{pageCurrent}_{pageSize}_{pageCount}")
	public String authorityManage(AuthorityEntity entity, @PathVariable Integer pageCurrent, @PathVariable Integer pageSize, @PathVariable Integer pageCount, Model model) throws Exception{
		//判断
		if(pageSize == 0) pageSize = 10;
		if(pageCurrent == 0) pageCurrent = 1;
		int rows = authorityService.count(entity);
		if(pageCount == 0) pageCount = rows%pageSize == 0 ? (rows/pageSize) : (rows/pageSize) + 1;

		//查询
		entity.setStart((pageCurrent - 1)*pageSize);
		entity.setEnd(pageSize);
		List<AuthorityEntity> list = authorityService.list(entity);
		if(list!=null && list.size()>0){
			for (int i=0;i<list.size();i++){
				list.get(i).setDataIndex(entity.getStart()+i+1);
			}
		}

		//输出
		model.addAttribute("list", list);
		String pageHTML = PageUtil.getPageContent("authorityManage_{pageCurrent}_{pageSize}_{pageCount}?name="+entity.getName(), pageCurrent, pageSize, pageCount);
		System.err.println("authorityManage  pageHTML="+pageHTML);
		model.addAttribute("pageHTML",pageHTML);
		model.addAttribute("authority",entity);
		return "zhsq/authorityManage";
	}

	/**
	 * 权限新增、修改跳转
	 * @param model
	 * @param entity * @return

	 */
	@PostMapping("/zhsq/authorityEditGetEntity")
	public String authorityEditGetEntity(Model model,AuthorityEntity entity) throws Exception{
		if(entity.getId()!=null){
			model.addAttribute("authority",authorityService.getAuthorityById(entity));
		}else{
			model.addAttribute("authority",new AuthorityEntity());
		}
		model.addAttribute("parentAuthoritys",comboboxService.listParentAuthoritysCombobox(null));
		return "zhsq/authorityEdit";
	}

	/**
	 * 权限新增、修改提交
	 * @param model
	 * @param entity
	 * @param httpSession
	 * @return
	 */
	@PostMapping("/zhsq/authorityEdit")
	public String authorityEditPost(Model model, AuthorityEntity entity, HttpSession httpSession, HttpServletRequest request)throws Exception {
		HttpSession session = request.getSession();
		SessionInfo sessioninfo = (SessionInfo)session.getAttribute("sessionInfo");
		if(entity.getId()!=null){
			authorityService.edit(entity);
			this.logService.insertLog(LogEntity.EDIT_TYPE, "修改权限数据"+entity.toString(), sessioninfo.getUser().getLoginName(), true);
		} else {
			authorityService.add(entity);
			this.logService.insertLog(LogEntity.INSERT_TYPE, "添加权限数据"+entity.toString(), sessioninfo.getUser().getLoginName(), true);
		}
		return "redirect:authorityManage_0_0_0";
	}

	@ResponseBody
	@PostMapping("/zhsq/authorityDelete")
	public ResultObject<Object> authorityDelete(IDEntity entity, HttpServletRequest request) throws Exception{
		authorityService.delete(entity.getIntID());
		HttpSession session = request.getSession();
		SessionInfo sessioninfo = (SessionInfo)session.getAttribute("sessionInfo");
		this.logService.insertLog(LogEntity.DELETE_TYPE, "删除权限数据"+entity.toString(), sessioninfo.getUser().getLoginName(), true);
		ResultObject<Object> object = new ResultObject<Object>(Constant.Code01, Constant.Msg01, null, null);
		return object;
	}
}
